var app = getApp()

Page({
    data: {
        activeIndex: 0
    },
    onLoad: function (t) {
        var a = this
        app.getUrl(a)
        app.getSystem(a)
        app.util.request({
                url: 'entry/wxapp/GetSystem',
                success: function (t) {
                    console.log(t, 'tttt55') - 1 < t.data.jd_custom.indexOf('查询') &&
                        (t.data.jd_custom = t.data.jd_custom.replace('查询', '')),
                        a.setData({
                            luntext: ['房型列表', t.data.jd_custom + '详情', t.data.jd_custom + '评价']
                        })
                }
            }),
            a.date(),
            a.setData({
                grade: wx.getStorageSync('platform').open_member
            })
        a.data.text
        var e = decodeURIComponent(t.scene)

        // 把seller_x_id和fx_x_user_id截取到然后放进obj

        var obj = {}
        var str = e.split('&')
        for (let i = 0; i < str.length; i++) {
            let a = str[i].split('=') // 小1
            obj[a[0]] = a[1] // 小2
        }
        for (var key in obj) {
            if (typeof obj[key] === 'string') {
                if (!isNaN(obj[key])) {
                    obj[key] = parseInt(obj[key])
                } else if (!isNaN(parseFloat(obj[key]))) {
                    obj[key] = parseFloat(obj[key])
                }
            }
        }
        console.log(e, obj, '是扫码的')
        // if (null == t.hotel_id) var i = e; else i = t.hotel_id; //原先的值
        if (null == t.hotel_id) var i = obj.seller_x_id
        else i = t.hotel_id
        var k
        if (undefined == obj.fx_x_user_id) k = 0
        else k = obj.fx_x_user_id
        console.log('酒店id为', i, '用户id为', k),
            a.setData({
                fx_x_user_id: k,
                hotel_id: i,
                start: app.util.time(),
                end: app.util.addDate(app.util.time(), 28)
            }),
            a.refresh()
        wx.getSetting({
            withSubscriptions: true, //  这里设置为true,下面才会返回mainSwitch
            success: function (res) {
                // 调起授权界面弹窗
                if (res.subscriptionsSetting.mainSwitch) {
                    // 用户打开了订阅消息总开关
                    if (res.subscriptionsSetting.itemSettings != null) {
                        // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
                        let moIdState =
                            res.subscriptionsSetting.itemSettings[
                                wx.getStorageSync('platform').tid1
                            ] // 用户同意的消息模板id
                        if (moIdState === 'accept') {
                            console.log('接受了消息推送')
                            wx.requestSubscribeMessage({
                                // 调起消息订阅界面
                                tmplIds: [wx.getStorageSync('platform').tid1],
                                success(res) {
                                    console.log('订阅消息 成功 ')
                                    console.log(res)
                                },
                                fail(er) {
                                    console.log('订阅消息 失败 ')
                                    console.log(er)
                                }
                            })
                        } else if (moIdState === 'reject') {
                            console.log('拒绝消息推送')
                        } else if (moIdState === 'ban') {
                            console.log('已被后台封禁')
                        }
                    }
                    // else {
                    //   // 当用户没有点击 ’总是保持以上选择，不再询问‘  按钮。那每次执到这都会拉起授权弹窗
                    //   wx.showModal({
                    //     title: '开通微信通知权限',
                    //     content: '建议开启微信通知，便于接收酒店订单消息提醒',
                    //     showCancel: true,
                    //     success: function (ress) {
                    //       if (ress.confirm) {
                    //         wx.requestSubscribeMessage({ // 调起消息订阅界面
                    //           tmplIds: [wx.getStorageSync("platform").tid1],
                    //           success(res) {
                    //             console.log('订阅消息 成功 ');
                    //             console.log(res);
                    //           },
                    //           fail(er) {
                    //             console.log("订阅消息 失败 ");
                    //             console.log(er);
                    //           }
                    //         })

                    //       }
                    //     }
                    //   })
                    // }
                } else {
                    console.log('订阅消息未开启')
                }
            },
            fail: function (error) {
                console.log(error)
            }
        })
    },
    date: function (t) {
        var a = wx.getStorageSync('day1'),
            e = wx.getStorageSync('day2'),
            i = wx.getStorageSync('day'),
            d = app.util.time()
        if ((wx.setStorageSync('datein', o), '' == a)) {
            var o = app.util.time()
            wx.setStorageSync('datein', o)
        } else if (a < d) o = d
        else o = a
        if ('' == e) var r = app.util.addDate(d, 1)
        else {
            var n = app.util.addDate(d, 1)
            if (e < n) r = n
            else r = e
        }
        i = app.util.day(r, o)
        wx.setStorageSync('day1', o),
            wx.setStorageSync('day2', r),
            wx.setStorageSync('day', i),
            this.setData({
                datein: o,
                dateout: r,
                time: i,
                current_date: o
            })
    },
    refresh: function (t) {
        var d = this,
            a = d.data.hotel_id,
            o = d.data.day1
        app.util.request({
            url: 'entry/wxapp/PjDetails',
            cachetime: '0',
            data: {
                seller_id: a
            },
            success: function (t) {
                if (t.data.address?.length >= 9) {
                    t.data.address = t.data.address.slice(0, 9) + '...'
                }

                const myLat = wx.getStorageSync('latitude')
                const myLng = wx.getStorageSync('longitude')
                const hotelLat = t.data.coordinates.split(',')[0]
                const hotelLng = t.data.coordinates.split(',')[1]
                t.data.distance = app.util.location(myLat, hotelLat, myLng, hotelLng)

                d.setData({
                    hotel: t.data
                })
            }
        })
        o = wx.getStorageSync('day1')
        var r = app.util.addDate(o, 1)
        app.util.request({
                url: 'entry/wxapp/RoomList',
                cachetime: '0',
                data: {
                    seller_id: a
                },
                success: function (t) {
                    var e = t.data
                    d.setData({
                        room: e
                    })
                    // a = function(a) {
                    //     app.util.request({
                    //         url: "entry/wxapp/GetRoomCost",
                    //         data: {
                    //             user_id:wx.getStorageSync("userInfo").id,
                    //             room_id: e[a].id,
                    //             start: o,
                    //             end: r
                    //         },
                    //         success: function(t) {
                    //             e[a].cost = t.data[0].mprice, d.setData({
                    //                 room: e
                    //             });
                    //         }
                    //     }), app.util.request({
                    //         url: "entry/wxapp/GetRoomNum",
                    //         data: {
                    //             room_id: e[a].id,
                    //             start: o,
                    //             end: r
                    //         },
                    //         success: function(t) {
                    //             e[a].room_num = t.data[0].nums, d.setData({
                    //                 room: e
                    //             });
                    //         }
                    //     });
                    // };
                    // for (var i in e) a(i);
                }
            }),
            app.util.request({
                url: 'entry/wxapp/AssessList',
                cachetime: '0',
                data: {
                    seller_id: a
                },
                success: function (t) {
                    for (var a in t.data)
                        '' != t.data[a].img && (t.data[a].img = t.data[a].img.split(',')),
                        (t.data[a].time = app.ormatDate(t.data[a].time).slice(0, 10)),
                        (t.data[a].content = t.data[a].content.replace('↵', '\n'))
                    d.setData({
                        assess_list: t.data.slice(0, 5)
                    })
                }
            })
    },
    tabClick: function (t) {
        this.setData({
            activeIndex: t.currentTarget.id
        })
    },
    bindDateChange1: function (t) {
        var a = this,
            e = t.detail.value,
            i = (a.data.dateout, a.data.current_date, app.util.addDate(e, 1))
        wx.setStorageSync('day1', e), wx.setStorageSync('day2', i), wx.setStorageSync('day', d)
        var d = app.getTime2Time(i, e)
        a.setData({
                datein: t.detail.value,
                dateout: i,
                time: d
            }),
            this.refresh()
    },
    bindDateChange2: function (t) {
        var a = this.data.datein,
            e = t.detail.value,
            i = app.getTime2Time(e, a)
        wx.setStorageSync('day1', a),
            wx.setStorageSync('day2', e),
            wx.setStorageSync('day', i),
            this.setData({
                dateout: t.detail.value,
                time: i
            })
    },
    room_info: function (t) {
        this.data.hotel.id
        var a = this.data.hotel,
            e = a.tel,
            i = a.coordinates,
            d = a.address,
            o = a.name
        wx.navigateTo({
            url: 'room_info?coordinates=' +
                i +
                '&room_id=' +
                t.currentTarget.dataset.id +
                '&tel=' +
                e +
                '&address=' +
                d +
                '&name=' +
                o +
                '&price=' +
                t.currentTarget.dataset.price
        })
    },
    order: function (t) {},
    all_comment: function (t) {
        wx.navigateTo({
            url: 'all_comment?seller_id=' + this.data.hotel_id
        })
    },
    sele_address: function (t) {
        var i = this.data.hotel,
            d = i.coordinates.split(',')
        wx.getLocation({
            type: 'gcj02',
            success: function (t) {
                var a = Number(d[0]),
                    e = Number(d[1])
                console.log(a, e, 'i555')
                wx.openLocation({
                    latitude: a,
                    longitude: e,
                    name: i.name,
                    address: i.address
                })
            }
        })
    },
    call_phone: function (t) {
        wx.makePhoneCall({
            phoneNumber: this.data.hotel.tel
        })
    },
    previewImage: function (t) {
        var a = this.data.url,
            e = [],
            i = t.currentTarget.id,
            d = t.currentTarget.dataset.index,
            o = this.data.assess_list
        for (var r in o)
            if (i == o[r].id) var n = o[r].img
        for (var s in n) e.push(a + n[s])
        wx.previewImage({
            current: a + n[d],
            urls: e
        })
    },
    formSubmit: function (t) {
        let a = t.detail.formId
        if (!app.globalData.hasRegister)
            wx.showModal({
                content: '您需要注册会员',
                success: function (t) {
                    t.confirm &&
                        wx.navigateTo({
                            url: '../register/register'
                        })
                }
            })
        else {
            var d = wx.getStorageSync('day1'),
                o = wx.getStorageSync('day2')
            // 现代码是酒店id和用户id
            1 == app.time_title(d, o) &&
                (1 == t.detail.target.dataset.classify ?
                    wx.navigateTo({
                        url: '../order_place/order_place?room_id=' +
                            t.detail.target.dataset.id +
                            '&fx_x_user_id=' +
                            this.data.fx_x_user_id +
                            '&hotel_id=' +
                            this.data.hotel_id +
                            '&form_d=' +
                            a
                    }) :
                    wx.navigateTo({
                        url: '../order_place/order_place?room_id=' +
                            t.detail.target.dataset.id +
                            '&fx_x_user_id=' +
                            this.data.fx_x_user_id +
                            '&hotel_id=' +
                            this.data.hotel_id +
                            '&form_d=' +
                            a
                    }))
            // 1 == app.time_title(d, o) && (1 == t.detail.target.dataset.classify ? wx.navigateTo({
            //   url: "../place_order/place_order?room_id=" + t.detail.target.dataset.id + "&fx_x_user_id=" + this.data.fx_x_user_id+"&hotel_id=" + this.data.hotel_id + "&form_d=" + a
            // }) : wx.navigateTo({
            //   url: "../place_order/place_order?room_id=" + t.detail.target.dataset.id + "&fx_x_user_id=" + this.data.fx_x_user_id+"&hotel_id=" + this.data.hotel_id + "&form_d=" + a
            // }));
            // 原先代码只有酒店id
            // 1 == app.time_title(d, o) && (1 == t.detail.target.dataset.classify ? wx.navigateTo({
            //   url: "../place_order/place_order?room_id=" + t.detail.target.dataset.id +"&hotel_id=" + this.data.hotel_id + "&form_d=" + a
            // }) : wx.navigateTo({
            //   url: "../place_order/place_order?room_id=" + t.detail.target.dataset.id +"&hotel_id=" + this.data.hotel_id + "&form_d=" + a
            // }));
            // 1 == app.time_title(d, o) && (1 == t.detail.target.dataset.classify ? wx.navigateTo({
            //     url: "../place_order/place_order?room_id=" + t.detail.target.dataset.id + "&hotel_id=" + this.data.hotel_id + "&form_d=" + a
            // }) : wx.navigateTo({
            //     url: "hour_room?room_id=" + t.detail.target.dataset.id + "&hotel_id=" + this.data.hotel_id + "&form_d=" + a + "&cost=" + t.detail.target.dataset.cost + "&rz_time=" + t.detail.target.dataset.rz_time
            // }));
        }
    },
    hotel_in: function (t) {
        wx.navigateTo({
            url: 'hotel_in?seller_id=' + t.currentTarget.dataset.id
        })
    },
    bindgetuserinfo: function (t) {
        'getUserInfo:fail auth deny' == t.detail.errMsg &&
            wx.showModal({
                title: '',
                content: '您拒绝了个人信息授权，无法正常使用小程序'
            })
    },
    onShareAppMessage: function () {
        return {
            title: this.data.hotel.name,
            path: 'zh_jdgjb/pages/hotel_list/hotel_info?hotel_id=' + this.data.hotel.id
        }
    }
})